package com.apolo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.adaptadores.Adaptador;
import com.conexiones.WebService;

import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;

public class Cursos extends Activity {
	
	private ListView lstCursos;
	private WebService webService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cursos);
		
		this.webService = new WebService();
		this.lstCursos = (ListView) findViewById(R.id.lstCursos);
		if(this.hayInternet()){
			new ObtenerCursos().execute();
		}
		else{
			Toast.makeText(this, "Necesitas una conexión a internet para ver los cursos disponibles.\n" +
					"Por favor intentalo de nuevo mas tarde.", Toast.LENGTH_LONG).show();
		}
	}
	
	public void mostrarRegistro(View view){
		Intent intent = new Intent(this, Registro.class);
		startActivity(intent);
	}
	
	public void ubicacion(View view){
		Intent intent = new Intent(this, Ubicacion.class);
		startActivity(intent);
		this.finish();
	}
	
	public void contacto(View view){
		Intent intent = new Intent(this, Contacto.class);
		startActivity(intent);
		this.finish();
	}
	
	public void informacion(View view){
		Intent intent = new Intent(this, Informacion.class);
		startActivity(intent);
		this.finish();
	}
	
	public void facebook(View view){
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/apolosoft?fref=ts"));
		startActivity(intent);
	}
	
	public void twitter(View view){
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/apolosoft"));
		startActivity(intent);
	}
	
	public void volverMenu(View view){
		this.finish();
	}
	
	private boolean hayInternet() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isAvailable()
				&& cm.getActiveNetworkInfo().isConnected()) {
			return true;
		} else {
			return false;
		}
	}
	
	private void mostrarCursos(JSONArray array){
		Adaptador adaptador = new Adaptador(this, array);
		this.lstCursos.setAdapter(adaptador);
	}
	
	private class ObtenerCursos extends AsyncTask<Void, Void, Boolean> {

		private Dialog dialog = new Dialog(Cursos.this);
		private JSONArray respuesta;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog.setTitle("Verificando cursos disponibles...");
			dialog.setCancelable(false);
	        dialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... arg0) {
			String[] parametros = { "mostrarCursos" };
			this.respuesta = webService.conectar(parametros);
			try {
				JSONObject res = respuesta.getJSONObject(0);
				if (res.getBoolean("respuesta")) {
					return true;
				} else {
					return false;
				}
			} catch (JSONException e) {
				return false;
			}
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			dialog.dismiss();
			if(result){
				mostrarCursos(this.respuesta);
			}
			else{
				Toast.makeText(getApplicationContext(), "En este momento no hay cursos disponibles.", Toast.LENGTH_LONG).show();
			}
		}
	}
}
